package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Userinfo;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.query.UserQueryObject;
import cn.wolfcode.crm.service.IUserinfoService;
import cn.wolfcode.crm.util.JSONResult;
import cn.wolfcode.crm.util.PageResult;
import cn.wolfcode.crm.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("studentInfo")
public class StudentInfoController {

    @Autowired
    private IUserinfoService userinfoService;

    @RequestMapping("view")
    public String queryByStudentName() {
        return "studentInfo";
    }

    @RequestMapping("login")
    public String login(String username, String password) {
        return "studentInfo";
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Userinfo userinfo) {
        JSONResult result = new JSONResult();
        try {
            userinfoService.saveOrUpdate(userinfo);
        } catch (Exception e) {
            result.remark(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 根据学生账户ID删除当前数据
     * @param id
     * @return
     */
    @RequestMapping("deleteByStudentId")
    @ResponseBody
    public JSONResult deleteByStudentId(Long id) {
        JSONResult result = new JSONResult();
        try {
            userinfoService.deleteByStudentId(id);
        } catch (Exception e) {
            result.remark(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("query")
    @ResponseBody
    public List<Userinfo> studentInfo(String studentName) {
        //String StudentName = UserContext.getStudentContext().getUsername();
        return userinfoService.queryByStudentName(studentName);
    }
}
